<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Core_Controllers_Auth extends Gio_Core_Controller
{
	public function loginAction()
	{
		$request  = $this->getRequest();
		$this->setLayout('login');
		$return = $request->getParam('return');
		$this->view->return = $return;
		
		if ($request->isPost()) {
			
			$username = $request->getPost('username');
			$password = $request->getPost('password');
			$this->view->username = $username;
			$this->view->password = $password;
			
			$user = Modules_Core_Services_User::auth($username, $password, true);
			if (null == $user) {
				$this->view->error = true;
				$this->view->message = $this->view->TRANSLATOR->translator('auth_login_error');
			} elseif ($user['status'] == 'inactive') {
				$this->view->error = true;
				$this->view->message = $this->view->TRANSLATOR->translator('auth_login_inactive');
			} elseif ($user['status'] == 'active') {
				$user['last_login'] = date('Y-m-d H:i:s');
				Modules_Core_Services_User::setLastLogin($user);
				
				$configs = Gio_Core_Config_Xml::getConfig('session');			
				$json = new Services_JSON();
				$sessionId = session_id();
				$session = array(
							'session_id' => $sessionId,
							'created_date' => strtotime(date('Y-m-d H:i:s')),
							'last_update' => strtotime(date('Y-m-d H:i:s')),
							'lifetime'	=> (isset($configs->lifetime))
													? $configs->lifetime
													: 3600,
							'inactive_time'	=> (isset($configs->inactive_time))
													? $configs->inactive_time
													: 120,
							'data' => $json->encodeUnsafe($user),
							'status' => 'active',
						);
						
				Gio_Core_Session::add($session);
				
				$return = $request->getPost('return');
				$redirect = ($return) ? base64_decode(rawurldecode($return)) : $this->view->url('core_dashboard_index');
				$this->redirect($redirect);
			}
		}
	}
	
	public function myopenidAction()
	{
		$this->disableLayout();
		$this->setNoRender();
		
		$request  = Gio_Core_Request::getInstance();
		$this->setLayout('login');
		
		require_once LIB_DIR . DS . 'openid' . DS . 'class.openid.v2.php';
		
		if ($request->isPost()) {
			$openidUrl = $request->getPost('openid_url');
			$openidUrl = rtrim($openidUrl, '/');
			
			$openid = new OpenIDService();
			$openid->SetIdentity($openidUrl);
			$openid->SetTrustRoot($request->getBaseUrl());
			$openid->SetRequiredFields(array('email', 'fullname'));
			$openid->SetOptionalFields(array('dob', 'gender', 'country'));
			
			if ($openid->GetOpenIDServer()) {
				$openid->SetApprovedURL($this->view->url('core_auth_myopenid'));
				$openid->Redirect();
			} else {
				$error = $openid->GetError();
				$error_code = $error["code"] ;
				$error_string = $error["description"];
			}
		} elseif ($request->getParam('openid_mode') == 'id_res'){		
			$openid = new OpenIDService();
			$openidUrl = $request->getParam('openid_identity');
			$openidUrl = rtrim($openidUrl, '/');
			$openid->SetIdentity($openidUrl);
			$openid_validation_result = $openid->ValidateWithServer();
			
			/**
			 * Login success
			 */
			if ($openid_validation_result == true) {
				$user = Modules_Core_Services_User::getByOpenID($openidUrl);
				if ($user) {
					$user['last_login'] = date('Y-m-d H:i:s');
					Modules_Core_Services_User::setLastLogin($user);
					
					$configs = Gio_Core_Config_Xml::getConfig('session');			
					$json = new Services_JSON();
					$sessionId = session_id();
					$session = array(
								'session_id' => $sessionId,
								'created_date' => strtotime(date('Y-m-d H:i:s')),
								'last_update' => strtotime(date('Y-m-d H:i:s')),
								'lifetime'	=> (isset($configs->lifetime))
														? $configs->lifetime
														: 3600,
								'inactive_time'	=> (isset($configs->inactive_time))
														? $configs->inactive_time
														: 120,
								'data' => $json->encodeUnsafe($user),
								'status' => 'active',
							);
							
					Gio_Core_Session::add($session);
					$this->redirect($this->view->url('core_dashboard_index'));
				}
			}
		}
	}
	
	public function logoutAction()
	{
		$this->disableLayout();
		$this->setNoRender();
		
		Gio_Core_Session::destroy();
		$this->redirect($this->view->url('core_auth_login'));
	}
}